package com.sskj.gocoin.v.activity;

import android.content.Context;
import android.content.Intent;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.flyco.tablayout.CommonTabLayout;
import com.flyco.tablayout.listener.CustomTabEntity;
import com.flyco.tablayout.listener.OnTabSelectListener;
import com.sskj.gocoin.R;
import com.sskj.gocoin.adapter.MyFragmentPagerAdapter;
import com.sskj.gocoin.common.App;
import com.sskj.gocoin.common.Constants;
import com.sskj.gocoin.m.bean.TabEntity;
import com.sskj.gocoin.p.base.BasePresenter;
import com.sskj.gocoin.utils.ClickUtil;
import com.sskj.gocoin.v.base.BaseActivity;
import com.sskj.gocoin.v.fragment.FabiBuyAndSellDetailFragment;

import java.util.ArrayList;

import butterknife.BindView;

public class FabiDetailActivity extends BaseActivity<BasePresenter> {
    @BindView(R.id.commonTabLayout)
    CommonTabLayout commonTabLayout;
    @BindView(R.id.viewPager)
    ViewPager viewPager;
    @BindView(R.id.ll_back)
    LinearLayout llBack;
    private String[] mTitles = {"出售记录", "购买记录"};
    private ArrayList<Fragment> mFragments;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_fabi_detail;
    }

    @Override
    public BasePresenter getPresenter() {
        return new BasePresenter();
    }

    public static void start(Context context) {
        Intent starter = new Intent(context, FabiDetailActivity.class);
        context.startActivity(starter);
    }

    public static void start(Context context, boolean isBuy) {
        Intent starter = new Intent(context, FabiDetailActivity.class);
        starter.putExtra(Constants.IS_BUY, isBuy);
        context.startActivity(starter);
    }

    @Override
    protected void initData() {
        boolean isBuy = getIntent().getBooleanExtra(Constants.IS_BUY, true);
        ClickUtil.click(llBack, this::finish);

        mFragments = new ArrayList<>();
        mFragments.add(FabiBuyAndSellDetailFragment.newInstance(false));
        mFragments.add(FabiBuyAndSellDetailFragment.newInstance(true));


        ArrayList<CustomTabEntity> mTabEntities = new ArrayList<>();
        for (int i = 0; i < mTitles.length; i++) {
            mTabEntities.add(new TabEntity(mTitles[i]));
        }
        commonTabLayout.setTabData(mTabEntities);

        commonTabLayout.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
                viewPager.setCurrentItem(position, true);
            }

            @Override
            public void onTabReselect(int position) {

            }
        });
        viewPager.setAdapter(new MyFragmentPagerAdapter(getSupportFragmentManager(), mFragments));

        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                commonTabLayout.setCurrentTab(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        if (isBuy) {
            viewPager.setCurrentItem(1);
        } else {
            viewPager.setCurrentItem(0);
        }

    }
}
